﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITAP.Core.Modelling.Primitives;
using ITAP.ContourBuilder.Components;

namespace ITAP.ContourBuilder.GUI
{
    public delegate void FormClosedDelegate(DialogResult result);

    public partial class FormInputArc : Form
    {
        private bool isArcInInnerBounds = true;

        public bool IsArcInInnerBounds
        {
            get { return isArcInInnerBounds; }
            set { isArcInInnerBounds = value; }
        }

        public int ArcHeight
        {
            get
            {
                return (int)numericUpDown.Value;
            }
        }

        public event EventHandler ArcHeightChanged;

        public event FormClosedDelegate FormArcHeightClosed;

        private ContourElement element;
        private double oldHeightValue;

        bool isInitialized = false;

        public FormInputArc(ContourElement element)
        {
            this.element = element;
            this.oldHeightValue = this.element.Height;

            InitializeComponent();
            numericUpDown.ValueChanged += new EventHandler(numericUpDown_ValueChanged);
            numericUpDown.Value = (decimal)this.element.Height;
            isInitialized = true;
        }

        void numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (!isInitialized)
                return;

            double prevValue = this.element.Height;
            ContourContainer container = this.element.GetRootContainer();
            this.element.Height = (double)numericUpDown.Value;
            if (isArcInInnerBounds && !container.InnerBoundingBox.Contains(this.element.BoundingBox))
            {
                this.element.Height = prevValue;
                this.isInitialized = false;
                numericUpDown.Value = (decimal)prevValue;
                this.isInitialized = true;
            }
            
            this.ArcHeightChanged(sender, e);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Close();
            this.FormArcHeightClosed.Invoke(DialogResult.OK);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.element.Height = oldHeightValue;
            this.Close();
            this.FormArcHeightClosed.Invoke(DialogResult.Cancel);
        }


    }
}
